///////////////////////////////////////////////////////////
//  CMainWindow.cpp
//  Implementation of the Class CMainWindow
//  Created on:      21-Sep-2013 9:35:49 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CMainWindow.h"
#include "UIFactory.h"
#include "CommandHeader.h"
#include "CurrentCommand.h"
#include "DataManager.h"
#include "stdafx.h"

using namespace UI;
using namespace UI::CMD;
using namespace IwNUI;


//For test with alert dialog
//CAlertDialogPtr g_dialog;

CMainWindow::CMainWindow()
{
 /* g_dialog = CreateAlertDialog(CAttributes()
    .Set("name",                    "AlertDialog")
    .Set("title",                   "s3eNUIAlertDialog")
    .Set("message",                 "Test")
    .Set("positiveButtonCaption",   "Yes")
    .Set("negativeButtonCaption",   "No"));*/
    //.Set("neutralButtonCaption",    "Maybe"));

  // Container view
  //this->m_mainWindow = CreateWindow();

  // Open data
  Data::DataManager::GetInstance()->Open();

  this->m_containerView = CreateView();

  InitialTabBar();
  InitialViewsAndCommands();

  this->SetChild(this->m_containerView);
}

CMainWindow::CMainWindow( IwNUI::CApp* app )
{
  // Container view
  this->m_app = app;

  //this->m_mainWindow = CreateWindow();

  // Open data
  Data::DataManager::GetInstance()->Open();

  this->m_containerView = CreateView();

  InitialTabBar();
  InitialViewsAndCommands();

//  this->m_app->ShowWindow(this->m_mainWindow);

  this->SetChild(this->m_containerView);
}


CMainWindow::~CMainWindow()
{
  // Save data before release
  DataManager::GetInstance()->Save();

  // Call release in control command
  ControlCommand::Release();

  // Call release in current command
  CurrentCommand::Release();

  // Call release in data manage
  DataManager::Release();
}

void CMainWindow::InitialTabBar()
{
  this->m_tabBarView = CreateView(CAttributes()
    .Set("backgroundColour", CColour(COLOUR_LIGHTGREY)));

  this->m_tabBar = CreateTabBar(CAttributes()
    .Set("name",    "MyTabBar")
    .Set("x1",      "0%")
    .Set("width",   "100%")
    .Set("alignH",  "bottom")
    .Set("height",  "10%")
    .Set("selected", 0) /*default selected template*/
    );

  // Setting event handler for handling tab selection
  this->m_tabBar->SetEventHandler("selecttab", this, &CMainWindow::OnSelectedTabBar);

  // Adding 3 tab bar items to the tab
  // Template view item
  CTabBarItemPtr item = CreateTabBarItem(CAttributes()
    .Set("name",    "TabBarItem1")
    .Set("caption", "Template")
    .Set("normal.image", "ico_table_off.png")
    .Set("pressed.image", "ico_table_on.png"));

  // add template view item to tab bar
  this->m_tabBar->AddChild(item);

  // Checklist view item
  item = CreateTabBarItem(CAttributes()
    .Set("name",    "TabBarItem2")
    .Set("caption", "CheckList")
    .Set("normal.image", "ico_buttons_off.png")
    .Set("pressed.image", "ico_buttons_on.png"));

  // add checklist view item to tab bar
  this->m_tabBar->AddChild(item);

  item = CreateTabBarItem(CAttributes()
    .Set("name",    "TabBarItem3")
    .Set("caption", "About")
    .Set("normal.image", "ico_about_off.png")
    .Set("pressed.image", "ico_about_on.png"));

  // add about view item to tab bar
  this->m_tabBar->AddChild(item);

  // add tab bar to view
  //this->m_tabBarView->AddChild(this->m_tabBar);

  // add tab bar view to container
  this->m_containerView->AddChild(this->m_tabBar/*this->m_tabBarView*/);
}

void CMainWindow::InitialViewsAndCommands()
{
  ////////////////////////////////////////////////////////////
  // Create all view
  ////////////////////////////////////////////////////////////
  CCheckListViewPtr checkListView = UIFactory::CreateCheckListView(DataManager::GetInstance()->GetCheckListPages(), this);
  CCreateCheckListViewPtr createCheckListView = UIFactory::CreateCreCheckListView(this);
  CChoicesViewPtr choicesView = UIFactory::CreateChoicesView(this);
  CPreviewDetailViewPtr previewDetailView = UIFactory::CreatePreviewDetailView(this);
  CSendMailViewPtr sendMailView = UIFactory::CreateSendMailView(this);
  CTemplateViewPtr templateView = UIFactory::CreateTemplateView(DataManager::GetInstance()->GetTemplatePages(), this);
  CCreateTemplateViewPtr createTemplateView = UIFactory::CreateCreTemplateView(this);
  CCreateChoiceViewPtr createChoiceView = UIFactory::CreateCreChoiceView(this);
  CCreateItemViewPtr createItemView = UIFactory::CreateCreItemView(this);
  CSaveViewPtr  saveView = UIFactory::CreateSaveView(this);
  CAboutViewPtr aboutView = UIFactory::CreateAboutView(this);
  
  ///////////////////////////////////////////////////////////////
  // Set visible all view
  //////////////////////////////////////////////////////////////
  checkListView->ShowView(false);
  createCheckListView->ShowView(false);
  choicesView->ShowView(false);
  previewDetailView->ShowView(false);
  sendMailView->ShowView(false);
  templateView->ShowView(true); // by default will show template view
  createTemplateView->ShowView(false);
  createChoiceView->ShowView(false);
  createItemView->ShowView(false);
  saveView->ShowView(false);
  aboutView->ShowView(false);

  ////////////////////////////////////////////////////////////////////
  // Add all view to container
  ////////////////////////////////////////////////////////////////////
  this->m_containerView->AddChild(checkListView);
  this->m_allViews.insert(std::make_pair(checkListView->GetType(), checkListView.get()));
  this->m_containerView->AddChild(createCheckListView);
  this->m_allViews.insert(std::make_pair(createCheckListView->GetType(), createCheckListView.get()));
  this->m_containerView->AddChild(choicesView);
  this->m_allViews.insert(std::make_pair(choicesView->GetType(), choicesView.get()));
  this->m_containerView->AddChild(previewDetailView);
  this->m_allViews.insert(std::make_pair(previewDetailView->GetType(), previewDetailView.get()));
  this->m_containerView->AddChild(sendMailView);
  this->m_allViews.insert(std::make_pair(sendMailView->GetType(), sendMailView.get()));
  this->m_containerView->AddChild(templateView);
  this->m_allViews.insert(std::make_pair(templateView->GetType(), templateView.get()));
  this->m_containerView->AddChild(createTemplateView);
  this->m_allViews.insert(std::make_pair(createTemplateView->GetType(), createTemplateView.get()));
  this->m_containerView->AddChild(createChoiceView);
  this->m_allViews.insert(std::make_pair(createChoiceView->GetType(), createChoiceView.get()));
  this->m_containerView->AddChild(createItemView);
  this->m_allViews.insert(std::make_pair(createItemView->GetType(), createItemView.get()));
  this->m_containerView->AddChild(saveView);
  this->m_allViews.insert(std::make_pair(saveView->GetType(), saveView.get()));
  this->m_containerView->AddChild(aboutView);
  this->m_allViews.insert(std::make_pair(aboutView->GetType(), aboutView.get()));
  //this->m_mainWindow->SetChild(this->m_containerView);

  ///////////////////////////////////////////////////////////////////
  // Create all command
  //////////////////////////////////////////////////////////////////
  // Get control command object
  ControlCommand* ctrlCommand = ControlCommand::GetInstance();
  // Create back checklist command and set to control command
  ctrlCommand->SetCommand(new BackCheckListCommand(createCheckListView.get(), templateView.get(), checkListView.get()));
  // Create back template command and set to control command
  ctrlCommand->SetCommand(new BackTemplateCommand(createTemplateView.get(), templateView.get()));
  // Create edit checklist command and set to control command
  ctrlCommand->SetCommand(new EditCheckListCommand(createCheckListView.get(), checkListView.get()));
  // Create edit choice command and set to control command
  ctrlCommand->SetCommand(new EditChoiceCommand(choicesView.get(), createCheckListView.get()));
  // Create edit item command and set to control command
  ctrlCommand->SetCommand(new EditItemCommand(createItemView.get(), createTemplateView.get()));
  // Create edit template command and set to control command
  ctrlCommand->SetCommand(new EditTemplateCommand(createTemplateView.get(), templateView.get()));
  // Create insert item command and set to control command
  ctrlCommand->SetCommand(new InsertItemCommand(createItemView.get(), createTemplateView.get()));
  // Create new checklist command and set to control command
  ctrlCommand->SetCommand(new NewCheckListCommand(createCheckListView.get(), templateView.get()));
  // Create new choice command and set to control command
  ctrlCommand->SetCommand(new NewChoiceCommand(createChoiceView.get(), createTemplateView.get()));
  // Create new item command and set to control command
  ctrlCommand->SetCommand(new NewItemCommand(createItemView.get(), createTemplateView.get()));
  // Create new template command and set to control command
  ctrlCommand->SetCommand(new NewTemplateCommand(createTemplateView.get(), templateView.get()));
  // Create save checklist command and set to control command
  ctrlCommand->SetCommand(new SaveCheckListCommand(createCheckListView.get(), checkListView.get(), saveView.get()));
  // Create save item command and set to control command
  ctrlCommand->SetCommand(new SaveItemCommand(createItemView.get(), createTemplateView.get()));
  // Create save template command and set to control command
  ctrlCommand->SetCommand(new SaveTemplateCommand(createTemplateView.get(), templateView.get(), saveView.get()));
  // Create send mail command and set to control command
  ctrlCommand->SetCommand(new SendMailCommand(sendMailView.get(), checkListView.get()));
  // Create view checklist command and set to control command
  ctrlCommand->SetCommand(new ViewCheckListCommand(previewDetailView.get(), checkListView.get()));
  // Create view template command and set to control command
  ctrlCommand->SetCommand(new ViewTemplateCommand(previewDetailView.get(), templateView.get()));
  // Create save choice command and set to control command
  ctrlCommand->SetCommand(new SaveChoiceCommand(choicesView.get(), createCheckListView.get()));
}

bool CMainWindow::OnSelectedTabBar(IwNUI::CTabBar* bar, int tab)
{
  //g_dialog->Show();
  std::map<UI::IViewCtrl::ViewType, IViewCtrl*>::iterator itView;
  itView = this->m_allViews.begin();
  while(itView != this->m_allViews.end())
  {
    itView->second->ShowView(false);
    itView++;
  }

  switch(tab)
  {
  case 0:
    this->m_allViews[IViewCtrl::TEMPLATEVIEW_T]->ShowView(true);
    break;
  case 1:
    this->m_allViews[IViewCtrl::CHECKLISTVIEW_T]->ShowView(true);
    break;
  case 2:
    this->m_allViews[IViewCtrl::ABOUTVIEW_T]->ShowView(true);
    break;
  default: break;
  }

  return true;
}

void CMainWindow::OnShowView(IViewCtrl* currentView, UI::IViewCtrl::ViewType nextView)
{
  switch(nextView)
  {
  case IViewCtrl::CREATECHECKLISTVIEW_T:
  case IViewCtrl::CHECKLISTVIEW_T:
    this->m_tabBar->SetAttribute("selected", 1);
    break;
  case IViewCtrl::TEMPLATEVIEW_T:
    this->m_tabBar->SetAttribute("selected", 0);
    break;
  default: break;
  }
  if(currentView)
    currentView->ShowView(false);
  this->m_allViews[nextView]->ShowView(true);
}

bool UI::CMainWindow::HideKeyboard()
{
  return this->m_app->DeactivateInput();
}
